<?php

namespace App\Service\Message;

use App\Constants\ErrorCode;
use App\Helpers\ImUtil;
use App\Model\User;
use App\Model\UserChat;
use App\Service\BaseService;

/**
 * 一键打招呼
 */
class ChatQuickService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $user = $this->getUser();
        $chatUserIds = $this->request->input('chat_userids', '');
        $content = trim($this->request->input('content', ''));
        if (!$chatUserIds || !$content) return $this->setError(ErrorCode::PARAMETER_ERROR);
        $chatUserIds = explode(',', $chatUserIds);
        //添加聊友记录
        foreach ($chatUserIds as $chatUserId) {
            UserChat::addRecord($userId, $chatUserId);
        }
        //批量发送私信
        $imAccounts = User::getImAccounts($chatUserIds);
        foreach ($imAccounts as $imAccount) {
            ImUtil::sendTextMsg($imAccount, $content, $user->im_account);
        }
        return ['result' => 1];
    }
}
